package com.hxy.basic.service.impl;

import com.hxy.basic.mapper.BaseMapper;
import com.hxy.basic.query.BaseQuery;
import com.hxy.basic.query.util.PageList;
import com.hxy.basic.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *  基类 service
 *   @param <T>
 * @authorr  Hxy
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public abstract class BaseServiceImpl<T> implements IBaseService<T> {


    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 保存 新增
     * @param t
     */
    @Transactional
    @Override
    public void save(T t) {
        baseMapper.save(t);
    }
    /**
     * 删除
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    /**
     * 修改
     * @param t
     */
    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 查询单个
     * @param id
     */
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 查询所有
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 分页查询
     */
    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        Long total = baseMapper.queryCount(baseQuery);
        // 判断count是否为空  提高查询效率
        if (total == null){
            return new PageList<>();
        }
        // 查询当前页数据
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(total,data);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }


    /**
     * 查询单个
     * @param id
     * @return
     */
    @Override
    public T findOne(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);

    }

}
